package com.example.house

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.house.adapter.HouseOneAdapter
import com.example.house.databinding.ActivityHouseBinding
import com.example.house.entity.HouseEntity
import com.example.house.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/house/HouseActivity")
class HouseActivity : BaseMvvmActivity<ActivityHouseBinding>() {

    private val houseViewModel:HouseViewModel by viewModels()


    override fun initView() {
        mDataBinding.rg1.setOnCheckedChangeListener { group, checkedId ->
            if(checkedId == R.id.rb1){
                houseViewModel.getHouse(1)
            }else if(checkedId == R.id.rb2){
                houseViewModel.getHouse(2)
            }else if(checkedId == R.id.rb3){
                houseViewModel.getHouse(3)
            }else if(checkedId == R.id.rb4){
                houseViewModel.getHouse(4)
            }
        }
        houseViewModel.mHouseSuccess.observe(this){
            ToastUtils.showShort("显示成功")
            val beanBean = mutableListOf<HouseEntity>()
            val map = it.groupBy {
                it.area//根据面积排序
            }
            val list = map.keys.sorted()//根据面积排序
            var index = 1
            for (i in list){
                beanBean.add(HouseEntity(index,i,map.get(i)!!))
                index++
            }
            mDataBinding.houseRv.adapter=HouseOneAdapter(beanBean).apply {
                notifyDataSetChanged()
            }
        }
        mDataBinding.tvRenter.setOnClickListener {
            ARouter.getInstance().build("/house/PeopleActivity").navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        houseViewModel.getHouse(1)
    }

    override fun getLayoutId(): Int = R.layout.activity_house
}